package com.ruoyi.project.service.impl;

import com.ruoyi.experiment.domain.Project;
import com.ruoyi.project.mapper.ProjectMapper;
import com.ruoyi.project.model.ContractCode;
import com.ruoyi.project.model.PLeader;
import com.ruoyi.project.service.IProjectService;
import com.ruoyi.project.utils.ProjectCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-15
 */
@Service
public class ProjectServiceImpl implements IProjectService
{
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectCode projectCode;

    /**
     * 查询项目
     *
     * @param projectId 项目主键
     * @return 项目
     */
    @Override
    public Project selectProjectByProjectId(Long projectId)
    {
        return projectMapper.selectProjectByProjectId(projectId);
    }

    /**
     * 查询项目列表
     *
     * @param project 项目
     * @return 项目
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {
        return projectMapper.selectProjectList(project);
    }

    @Override
    public int insertProject(Project project) {
        project.setCode(
                projectCode.getCode(
                        project.getProjectName(),
                        project.getProjectOwner(),
                        project.getType()==null?"HT":project.getType()));
        //所有合同默认有合同
        project.setHaveContract(1);
        return projectMapper.insertProject(project);
    }

    /**
     * 修改项目
     *
     * @param project 项目
     * @return 结果
     */
    @Override
    public int updateProject(Project project)
    {
        if (project!=null && project.getCode()!=null && project.getType()!=null){
            project.setCode(project.getType()+project.getCode().substring(2));
        }
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目
     *
     * @param projectIds 需要删除的项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectIds(Long[] projectIds)
    {
        for (Long projectId : projectIds) {
            Integer i = projectMapper.selectSamplesByPid(projectId);
            if (i>0)
                return -1;
        }
        for (Long projectId : projectIds) {
            projectMapper.removePayment(projectId);
        }
        return projectMapper.deleteProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目信息
     *
     * @param projectId 项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectId(Long projectId)
    {
        return projectMapper.deleteProjectByProjectId(projectId);
    }

    @Override
    public List<ContractCode> listContractCode() {
        return projectMapper.selectContractCode();
    }

    @Override
    public List<PLeader> listLeader() {
        return projectMapper.selectLeader();
    }
}

